<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of AmazonOrderController
 *
 * @author zhouyehao
 */
class AmazonOrderController extends AdminController {

    /**
     * 列表
     */
    public function index() {
        $where = array();
        //日期选择
        $select_date = I("select_date");
        if (!empty($select_date)) {
            $where['te.addtime'] = array('like', $select_date . '%');
            $this->assign("select_date", $select_date);
        }

        $amazon_order_id = I("amazon_order_id");
        if (!empty($amazon_order_id)) {
            $where['mo.amazon_order_id'] = $amazon_order_id;
            $this->assign("amazon_order_id", $amazon_order_id);
        }


        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");

        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where['mo.purchase_date'] = array("between", array($start_date, $end_date . ' 23:59:59'));
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
                $where['mo.purchase_date'] = array('EGT', $start_date);
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
                $where['mo.purchase_date'] = array('ELT', $end_date . ' 23:59:59');
                $this->assign("end_date", $end_date);
            }
        }

        $M = M()->table("lt_mws_orders mo");

        $list = $this->lists($M, $where, 'mo.purchase_date desc', 'mo.*');

        //获取SKU  计算发货数量等
        if (!empty($list)) {
            foreach ($list as &$val) {
                $res = M('MwsOrderItems')->where(array('amazon_order_id' => $val['amazon_order_id']))->select();
                $sku = [];
                $shipping_price = 0;
                $quantity_ordered = 0;
                if (!empty($res)) {
                    foreach ($res as $row) {
                        $sku[] = $row['seller_sku'];
                        $shipping_price += $row['shipping_price'];
                        $quantity_ordered += $row['quantity_ordered'];
                    }
                }

                $val['seller_sku'] = implode(',', $sku);
                $val['shipping_price'] = $shipping_price;
                $val['quantity_ordered'] = $quantity_ordered;
            }
            unset($val);
        }

        $this->assign('_list', $list);
        $this->meta_title = '亚马逊订单列表';
        $this->display();
    }

    //订单详情
    public function info() {
        $amazon_order_id = I("id");
        $data = M('MwsOrders')
                ->field(
                        'amazon_order_id,'
                        . 'purchase_date,'
                        . 'last_update_date,'
                        . 'order_type,'
                        . 'order_status,'
                        . 'number_of_items_shipped,'
                        . 'number_of_items_unshipped,'
                        . 'order_total,'
                        . 'buyer_name,'
                        . 'buyer_email,'
                        . 'shipping_address,'
                        . 'fulfillment_channel')
                ->where('amazon_order_id', $amazon_order_id)
//                ->where('uid', $this->auth->id)
                ->find();
        if (!$data) {
            $this->error('木有订单数据');
        }

        $address = (array) json_decode($data['shipping_address']);
        $data['CountryCode'] = $address['CountryCode'];
        $data['StateOrRegion'] = $address['StateOrRegion'];
        $data['City'] = $address['City'];
        $data['PostalCode'] = $address['PostalCode'];

        $strAddress = '';
        if (isset($address['AddressLine1'])) {
            $strAddress .= $address['AddressLine1'];
        }

        if (isset($address['AddressLine2'])) {
            $strAddress .= $address['AddressLine2'];
        }

        $data['Address'] = $strAddress;

        unset($data['shipping_address']);
        unset($data['order_status_text']);
        unset($data['fulfillment_channel_text']);
        unset($data['order_type_text']);


        $item = M('MwsOrderItems')->where(array('amazon_order_id' => $amazon_order_id))->select();
        $this->assign("data", $data);
        $this->assign("_list", $item);
        $this->display();
    }

}
